#include<cstring>
#include<cstdio>
#include<iostream>
#include<map>
using namespace std;
const int INF=0x3f3f3f3f;
int f[10000],price[7],fac[7],k[7],inf[7];
int b,n,state;
struct node
{
	int n;
	int k[6];
	int p;
}s[100];
map<int,int>mp;
int min(int x,int y){return x<y?x:y;}
void decode(int s,int inf[])
{
	int t=1;
	while(s>0)
	{
		inf[t]=s%6;
		s/=6;
		t++;
	}
}
void init()
{
	scanf("%d",&b);
	fac[0]=1;
	for(int i=1;i<=6;i++)
		fac[i]=fac[i-1]*6;
	for(int i=1;i<=b;i++)
	{
		int c;
		scanf("%d%d%d",&c,&k[i],&price[i]);
		mp[c]=i;
	}
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&s[i].n);
		int c,tem;
		for(int j=1;j<=b;j++)s[i].k[j]=0;
		for(int j=1;j<=s[i].n;j++)
		{
			scanf("%d%d",&c,&tem);
			s[i].k[mp[c]]=tem;
		}
		scanf("%d",&s[i].p);
	}
	state=0;
	for(int i=1;i<=b;i++)
		state+=k[i]*fac[i-1];
}
void work()
{
	f[0]=0;
	for(int i=1;i<=state;i++)
	{
		memset(inf,0,sizeof(inf));
		decode(i,inf);
		bool ff=false;
		for(int j=1;j<=b;j++)
			if(inf[j]>k[j]){f[i]=INF;ff=true;}
		if(ff)continue;
		f[i]=0;
		for(int j=1;j<=b;j++)
			f[i]+=price[j]*inf[j];
		for(int j=1;j<=n;j++)
		{
			int ss=0;
			bool flag=true;
			for(int p=1;p<=b;p++)
			{
				if(s[j].k[p]<=inf[p])ss+=fac[p-1]*s[j].k[p];
				else {flag=false;break;}
			}
			if(flag)f[i]=min(f[i],f[i-ss]+s[j].p);
		}
	}
	cout<<f[state]<<endl;
}
int main()
{
	init();
	work();
	return 0;
}

